package cn.cdeden.knowledge.controller.online;

import cn.cdeden.common.core.domain.R;
import cn.cdeden.common.satoken.utils.LoginHelper;
import cn.cdeden.knowledge.controller.online.factory.jurisdiction.Jurisdiction;
import cn.cdeden.knowledge.controller.online.factory.jurisdiction.JurisdictionFactory;
import cn.cdeden.knowledge.controller.online.factory.jurisdiction.JurisdictionResponse;
import cn.cdeden.knowledge.domain.TreeDownloadFiles;
import cn.cdeden.knowledge.domain.dto.DownloadFileRedisDto;
import cn.cdeden.knowledge.domain.dto.OnlinePreviewInfoDto;
import cn.cdeden.knowledge.utils.MyUtil;
import cn.cdeden.knowledge.utils.RedisNewUtils;
import cn.cdeden.system.api.model.LoginUser;
import com.alibaba.fastjson.JSONObject;
import io.undertow.util.BadRequestException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * 文件下载文件处理模块
 */
@Controller
@Slf4j
//@Api(tags = "文件下载文件处理模块")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/online")
public class DownloadFileController {

    private static final String online_Config = "file::download:";

    private static final int time = 10;

    private final JurisdictionFactory factory;

    private final RedisNewUtils redisNewUtils;

    /**
     * 下载模块:新的下载文件
     * @param info
     * @return
     */
//    @Log("下载模块:新的下载文件")
//    @ApiOperation("下载模块:新的下载文件")
    @ResponseBody
    @PostMapping(value = "/new/download/file")
    public R newDownloadFile(@RequestBody OnlinePreviewInfoDto info) throws BadRequestException {
        LoginUser user = null;
        try {
            user = LoginHelper.getLoginUser();
//            user = FileUserUtils.getUser();
        }catch (Exception e){
            throw new BadRequestException("必须登录才可以下载文件");
        }

        String type = info.getType();
        String id = info.getId();
        Jurisdiction jurisdiction = factory.getJurisdiction(type);
        if (ObjectUtils.isEmpty(jurisdiction)) {
            throw new BadRequestException("错误请求类型");
        }
        JurisdictionResponse preview = jurisdiction.getFileJurisdictionDownloadFile(id,info.getInfo());
        if (!preview.getState()) {
            throw new RuntimeException(preview.getMessage());
        }

        TreeDownloadFiles data = TreeDownloadFiles.builder()
            .id(Long.valueOf(id))
            .realName(preview.getName())
            .suffix(preview.getSuffix())
            .type(preview.getType())
            .edition(info.getEdition())
            .storageId(preview.getId())
            .build();

        //需要验证文件在文件中是否存在
        String hash = MyUtil.getUUID();
        String dataStr = JSONObject.toJSONString(
            DownloadFileRedisDto.builder()
                .userId(String.valueOf(user.getUserId()))
                .type(type)
                .hash(hash)
                .file(data)
                .build()
        );


        String string = online_Config +":"+user.getUserId()+":"+ hash;
        redisNewUtils.set(string,dataStr,time, TimeUnit.SECONDS);
//        redisUtils.set(string,dataStr);

        String url ="/api/online/download/single/"+user.getUserId()+"/" + hash;
        return R.ok(url);
//        return BaseRespResult.successResult(url);
    }
}
